package com.xk857.simpleeffect.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.AddProjectActivity;
import com.xk857.simpleeffect.adapter.ListProjectAdapter2;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.vo.ProjectVO;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;

import java.lang.reflect.Type;
import java.util.List;
import java.util.function.Consumer;

public class ProjectFragment extends Fragment {
    private View view;
    private RoundButton addBtn;
    private RecyclerView recyclerList;
    private ConstraintLayout emptyLayout;
    private RefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_fragment_project, container, false);
        initView(view);
        initRefresh();
        return view;
    }

    private void initData() {
        Type type = new TypeToken<Result<List<ProjectVO>>>() {}.getType();
        OkHttpUtils.getInstance().get(getActivity(), RequestEnum.GET_ME_PROJECT, type, false, (Consumer<List<ProjectVO>>) projectList -> {
            refreshLayout.finishRefresh();
            if (getActivity() != null) {
                if (projectList.size() > 0) {
                    getActivity().runOnUiThread(() -> {
                        ListProjectAdapter2 adapter = new ListProjectAdapter2(getActivity(), projectList, b -> {
                            if (getActivity() != null && b) {
                                getActivity().runOnUiThread(this::initData);
                            }
                        });
                        adapter.setItemAnimation(BaseQuickAdapter.AnimationType.AlphaIn);
                        recyclerList.setAdapter(adapter);
                        recyclerList.setVisibility(View.VISIBLE);
                        emptyLayout.setVisibility(View.GONE);
                    });
                } else {
                    getActivity().runOnUiThread(() -> {
                        recyclerList.setVisibility(View.GONE);
                        emptyLayout.setVisibility(View.VISIBLE);
                    });
                }
            }
        });
    }

    private void initRefresh() {
        refreshLayout.setEnableHeaderTranslationContent(true);
        //设置 Header 为 贝塞尔雷达 样式
        if (getActivity() != null) {
//            refreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setPrimaryColorId(R.color.main).setEnableHorizontalDrag(true));
            refreshLayout.setEnableHeaderTranslationContent(false);
            refreshLayout.setRefreshHeader(new MaterialHeader(getActivity()).setColorSchemeResources(R.color.main));
        }
        // 下拉刷新
        refreshLayout.setOnRefreshListener(mLayout -> initData());
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(View view) {
        addBtn = view.findViewById(R.id.add_btn);
        addBtn.setOnClickListener(v -> startActivity(new Intent(getActivity(), AddProjectActivity.class)));
        recyclerList = view.findViewById(R.id.recycler_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerList.setLayoutManager(layoutManager);
        TextView addEmptyText = view.findViewById(R.id.add_empty_text);
        addEmptyText.setOnClickListener(v -> startActivity(new Intent(getActivity(), AddProjectActivity.class)));
        emptyLayout = view.findViewById(R.id.empty_layout);
        refreshLayout = view.findViewById(R.id.refreshLayout);
    }
}
